<?php


/*
 * Created on 23/09/2008
 *
 * Utility methods for the display of matches
 */

function showMatchResultHeader() {
    echo<<< EOQ
<tr>
  <th align="center" rowSpan="2" class="strong">Date</th>
  <th align="center" rowSpan="2" class="strong">Home</th>
  <th align="center" colspan="2" class="strong">Touch&nbsp;Downs</th>
  <th align="center" rowSpan="2" class="strong">Away</th>
  <th align="center" rowSpan="2" class="strong">Gate</th>
  <th align="center" rowSpan="2" class="strong">&nbsp;</th>
</tr>
<tr>
  <td align="center" colspan="2" class="small">Casualties</td>
</tr>
EOQ;
}

function showMatchResult($match, & $rowcount, $editableComments = true) {
    global $teams, $loggedin;
    $homeTeam = $teams->getTeam($match->homeRec->teamId);
    $awayTeam = $teams->getTeam($match->awayRec->teamId);
    $ad = new ApprovalDao();
    $hasApprovals = $ad->findOutstandingMatchApprovals($match);
    if ($hasApprovals) {
        $hasApprovals = count($hasApprovals) > 0;
    }
    if ($hasApprovals) {
        echo getEchoTR(++ $rowcount);
        echo "<td align=\"center\" colSpan=7><i><a href=\"match_view.php?matchId=" . $match->id . "\">Unapproved</a></i></td></tr>";
    }
    echo getEchoTR(++ $rowcount);
    echo "<th align=center rowSpan=2>" . getEchoValue($match->datePlayed);
    echo "<br /><a href=\"match_view.php?matchId=" . $match->id . "\">Details</a></th>\n";
    echo "<td align=center rowSpan=2><b>" . getEchoValue($homeTeam->getRosterLink()) . "<br />(" . getEchoValue($homeTeam->coach->getName()) . ")</b></td>";
    echo "<th align=center>" . getEchoValue($match->homeRec->score) . "</th>";
    echo "<th align=center>" . getEchoValue($match->awayRec->score) . "</th>";
    echo "<td align=center rowSpan=2><b>" . getEchoValue($awayTeam->getRosterLink()) . "<br />(" . getEchoValue($awayTeam->coach->getName()) . ")</b></td>";
    echo "<td align=center>" . getEchoValue($match->type) . "</td>";
    echo "</tr>\n";
    echo getEchoTR($rowcount);
    echo "<td align=right><font size=-1>" . getEchoValue($match->homeRec->casualties) . "</font></td>";
    echo "<td align=left><font size=-1>" . getEchoValue($match->awayRec->casualties) . "</font></td>";
    echo "<td align=center>" . getEchoValue($match->gate) . "</td>";
    echo "</td></tr>\n";

    showComments($rowcount, $match->homeRec->comments, $homeTeam->coach, $match->awayRec->comments, $awayTeam->coach, $match->id);
    echo getEchoTR($rowcount);
    echo "<td colspan=6>&nbsp;</td></tr>";
}

function showCommentLine(& $rowcount, $comment, $coach, $matchRecord = false) {
    global $loggedin;
        echo getEchoTR(++ $rowcount);
        echo<<< EOQ
<td colspan="6" class="small">
EOQ;
    $canEdit = $loggedin && $matchRecord && ($loggedin->id == $coach->id);
    if ($canEdit) {
        echo<<< EOQ
<div id="Edit$matchRecord" style="display: none">
  <table width="100%" cellspacing="5">
    <tr>
	  <form method="post">
	  <input type="hidden" name="mrid" value="$matchRecord">
      <td class="small" align="center">
      <input type="submit" name="submit" value="Update\nComment">
      <br /><br /><a href="javascript:comment()">Cancel</a> 
      </td>
      <td align="left"><TEXTAREA name="comments" rows="5" cols="100">$comment</TEXTAREA></td>
      </form>
    </tr>
  </table>
</div>
<script language=javascript type='text/javascript'>
function edit() {
	changeDivDisplay('Edit$matchRecord', 'block');
	changeDivDisplay('Comment$matchRecord', 'none');
}

function comment() {
	changeDivDisplay('Edit$matchRecord', 'none');
	changeDivDisplay('Comment$matchRecord', 'block');
}
</script> 
EOQ;
    }
    if (!isset ($comment)) {
    	$comment = "";
    }
    $coachName = $coach->getName();
    echo<<< EOQ
<div id="Comment$matchRecord" style="display: block">        
<em>$coachName</em>: 
EOQ;
    if ($canEdit) {
        echo<<< EOQ
		(<a href="javascript:edit()">Edit</a>)&nbsp;
EOQ;
    }
    echo<<< EOQ
    <pre class="wrap">$comment</div></pre>
</div>
</td>
</tr>

EOQ;
    return $rowcount;
}

function showComments(& $rowcount, $homeComment, $homeCoach, $awayComment, $awayCoach, $matchId = false) {
    showCommentLine($rowcount, $homeComment, $homeCoach, $matchId ? ((2 * $matchId) - 1) : false);
    showCommentLine($rowcount, $awayComment, $awayCoach, $matchId ? (2 * $matchId) : false);
    return $rowcount;
}

function echoWinnings($selectName, $value) {
    echo<<<EOQ
    <select name="$selectName">
EOQ;
    for ($i = 0; $i <= 240000; $i += 10000) {
        echo "<option value=\"" . $i . "\"";
        if ($i == $value) {
            echo " selected ";
        }
        echo ">" . $i . "</option>\n";
    }
    echo<<<EOQ
    </select>
EOQ;
}

function echoFans($selectName, $value) {
    echo<<<EOQ
    <select name="$selectName">
EOQ;
    for ($i = 1; $i >= -1; $i--) {
        echo "<option value=\"" . $i . "\"";
        if ($i == $value) {
            echo " selected";
        }
        echo ">" . getFanText($i) . "</option>\n";
    }
    echo<<<EOQ
    </select>
EOQ;

}
?>
